//
//  WelcomeViewController.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/19.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit
import SDWebImage

/// 欢迎界面
class WelcomeViewController: UIViewController {

    
    override func loadView() {
        
        view = imgView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        iconView.sd_setImageWithURL(OauthViewModel.sharedOauthViewModel.avatarUrl, placeholderImage: OauthViewModel.sharedOauthViewModel.defaulImage)
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        titleLbl.alpha = 0
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(imgView.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.imgView.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.7, animations: { () -> Void in
                    self.titleLbl.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwiftNotificationKey, object: nil)
                })
        }
    }
    
    ///  设置UI界面
    private func setupUI(){
     
        // 添加子控件
        imgView.addSubview(iconView)
        imgView.addSubview(titleLbl)
        
        // 自动布局
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imgView.snp_centerX)
            make.bottom.equalTo(imgView.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        titleLbl.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.centerX.equalTo(iconView.snp_centerX)
        }
    }
    //MARK: - 懒加载
    /// 背景视图
    private lazy var imgView:UIImageView = UIImageView(imageName: "ad_background")
    
    /// 头像
    private lazy var iconView:UIImageView = {
        
        let iconView = UIImageView(imageName: "avatar_default_big")
        
        iconView.layer.cornerRadius = 45
        
        iconView.clipsToBounds = true
        
        return iconView
        }()
    
    
    
    /// 文字
    private lazy var titleLbl:UILabel = UILabel(titleText: "欢迎归来", textFont: 17)
}
